#include <iostream>
#include <fstream>
#include <string>
#include "contacts.pb.h"
using namespace std;

void PrintfContact(contacts::contact& conn) {
    for (int i = 0; i < conn.contacts_size(); i++) {
        cout << "----------------------联系人" << i + 1 << "----------------------" << endl;
        const contacts::PeopleInfo& people = conn.contacts(i);
        cout << "姓名: " << people.name() << endl;
        cout << "年龄: " << people.age() << endl;
        for (int j = 0; j < people.phone_numbers_size(); j++) {
            cout << "电话" << j + 1 << ": " << people.phone_numbers(j).number();
            cout << "(";
            cout << people.phone_numbers(j).PhoneType_Name(people.phone_numbers(j).type()) << ")" << endl;
        }
        if (people.has_data() && people.data().Is<contacts::Address>()) { // 判断是否设置了数据并且数据的类型正确
            contacts::Address addr;
            people.data().UnpackTo(&addr); // 将Any类型转换成Address类型，并将信息放到Address对象里面
            if (!addr.home_address().empty()) {
                cout << "家庭地址: " << addr.home_address() << endl;
            }
            if (!addr.unit_address().empty()) {
                cout << "单位地址: " << addr.unit_address() << endl;
            }
        }
    }

}

int main() {
    contacts::contact conn;
    // 打开文件
    fstream input("contacts.txt", ios::in | ios::binary);
    if (!input.is_open()) {
        cerr << "open file contacts.txt fail" << endl;
        return -1;
    }
    
    if (!conn.ParseFromIstream(&input)) {
        cerr << "parse fail!" << endl;
        input.close();
        return -1;
    }
    PrintfContact(conn);
    input.close();
    return 0;
}